import Vue from 'vue';
let _debounceTimeout = null,
	_throttleRunning = false
 // 防抖
const debounce = (fn, delay = 500) => {
	clearTimeout(_debounceTimeout);
	_debounceTimeout = setTimeout(() => {
		fn();
	}, delay);
}

// 节流
const throttle = (fn, delay = 500) => {
	if (_throttleRunning) {
		return;
	}
	_throttleRunning = true;
	fn();
	setTimeout(() => {
		_throttleRunning = false;
	}, delay);
}

// Toast
const msg = (title = '', param = {}) => {
	if (!title) return;
	uni.showToast({
		title,
		duration: param.duration || 1500,
		mask: param.mask || false,
		icon: param.icon || 'none'
	});
}
//减法函数
const accSub =  (arg1, arg2)=> {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	//last modify by deeka
	//动态控制精度长度
	n = r1 >= r2 ? r1 : r2;
	return parseFloat(((arg2 * m - arg1 * m) / m).toFixed(n));
}
// 判断类型集合
const checkStr = (str, type) => {
	switch (type) {
		case 'mobile': //手机号码
			return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
		case 'tel': //座机
			return /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(str);
		case 'card': //身份证
			return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
		case 'mobileCode': //6位数字验证码
			return /^[0-9]{6}$/.test(str);
		case 'pwd': //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
			return /^([a-zA-Z0-9_]){6,18}$/.test(str);
		case 'payPwd': //支付密码 6位纯数字
			return /^[0-9]{6}$/.test(str);
		case 'postal': //邮政编码
			return /[1-9]\d{5}(?!\d)/.test(str);
		case 'QQ': //QQ号
			return /^[1-9][0-9]{4,9}$/.test(str);
		case 'email': //邮箱
			return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
		case 'money': //金额(小数点2位)
			return /^\d*(?:\.\d{0,2})?$/.test(str);
		case 'URL': //网址
			return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
		case 'IP': //IP
			return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
		case 'date': //日期时间
			return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) ||
				/^(\d{4})\-(\d{2})\-(\d{2})$/
				.test(str)
		case 'number': //数字
			return /^[0-9]$/.test(str);
		case 'english': //英文
			return /^[a-zA-Z]+$/.test(str);
		case 'chinese': //中文
			return /^[\\u4E00-\\u9FA5]+$/.test(str);
		case 'lower': //小写
			return /^[a-z]+$/.test(str);
		case 'upper': //大写
			return /^[A-Z]+$/.test(str);
		case 'HTML': //HTML标记
			return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
		case 'pwd2': //验证字符串必须包含字母、数字、_ 长度 6 - 16
			return /^(?=.*\d+)(?=.*_+)(?=.*[A-Za-z]+)[\w]{6,16}$/.test(str);
		case 'pwd1': //验证字符串必须包含字母、数字、
			return /^(?=.*\d+)(?=.*[A-Za-z]+)[\w]{8,16}$/.test(str);
		case 'pwd3': //验证字符串必须包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
			return /^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*?]).{6,16}$/.test(str);
		case 'bank': //银行卡首位非0，位数也不同
			return /^([1-9]{1})(\d{14}|\d{18})$/.test(str);
		default:
			return true;
	}
}
const changeHead = (scroll,statusBarHeight) =>{
	if(scroll>statusBarHeight){
		uni.setNavigationBarColor({
		    frontColor: '#000000',
		    backgroundColor:"#ffffff",
		})
	}else{
		uni.setNavigationBarColor({
		    frontColor: '#ffffff',
		    backgroundColor: 'transparent', // 将背景颜色设置为透明
		})
	}
}
/**
 * 获取任意时间
 */
const getDate = (date, AddDayCount = 0)=> {
	if (!date) {
		date = new Date()
	}
	if (typeof date !== 'object') {
		date = date.replace(/-/g, '/')
	}
	const dd = new Date(date)

	dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

	const y = dd.getFullYear()
	const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
	const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
	return {
		fullDate: y + '-' + m + '-' + d,
		year: y,
		month: m,
		date: d,
		day: dd.getDay()
	}
}

const getWeek = (date)=>{
	var date = new Date(date); //定义日期对象
	var day = date.getDay(); //获取星期几
	console.log(day)
	switch (day) {
	    case 0:
			return '周日';
	        break;
	    case 1:
			return '周一';
	        break;
	    case 2:
			return '周二';
	        break;
	    case 3:
			return '周三';
	        break;
	    case 4:
			return '周四';
	        break;
	    case 5:
			return '周五';
	        break;
	    case 6:
			return '周六';
	        break;
	}
}
const formatDateTime = (inputTime)=> {  
	if(inputTime.toString().length<13){
		inputTime = Number(inputTime)*1000
	}
    var date = new Date(Number(inputTime));  //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var y = date.getFullYear();    
    var m = date.getMonth() + 1;    
    m = m < 10 ? ('0' + m) : m;    
    var d = date.getDate();    
    d = d < 10 ? ('0' + d) : d;    
    var h = date.getHours();  
    h = h < 10 ? ('0' + h) : h;  
    var minute = date.getMinutes();  
    var second = date.getSeconds();  
    minute = minute < 10 ? ('0' + minute) : minute;    
    second = second < 10 ? ('0' + second) : second;   
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;    
}
// tab切换语言
const changeFooterLang = () =>{
	uni.setTabBarItem(
		{
		  index: 0,
		  text: Vue.prototype._i18n.t('common').首页,
		},
	)
	uni.setTabBarItem(
		{
		  index: 1,
		  text: Vue.prototype._i18n.t('common').粉丝管理,
		},
	)
	uni.setTabBarItem(
		{
		  index: 2,
		  text: Vue.prototype._i18n.t('common').我的,
		},
	)
}
module.exports = {
	debounce:debounce,
	throttle:throttle,
	msg:msg,
	checkStr:checkStr,
	accSub:accSub,
	changeHead:changeHead,
	getDate:getDate,
	getWeek:getWeek,
	formatDateTime:formatDateTime,
	changeFooterLang:changeFooterLang
}
